﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenLS.Spreadsheet.Formats.JsonStructures;
using Raven.Client;
using Raven.Client.Document;

namespace GenericGridControl.Model
{
    public static class BigDataTableStaticDAL
    {
        private static IDocumentStore dbStore;

        static BigDataTableStaticDAL()
        {

        }

        private static IDocumentStore DbStore
        {
            get
            {
                if (dbStore == null)
                {
                    dbStore = new DocumentStore { Url = "http://ravenDB.dataprovider.com", DefaultDatabase = "QuantumWorkspacesBigDataTable" };
                    dbStore.Initialize();
                }   
                return dbStore;
            }
        }

        public static bool CheckNameAvailability(string strTableName, bool isPublic)
        {
            // Create a new session, retrieve an entity, and change it a bit
            using (var session = DbStore.OpenSession())
            {
                JsonTableDocument currentDoc = session.Query<JsonTableDocument>()
                                                      .Where(x => x.CurrentTable.Name == strTableName)
                                                      .Where(x => x.CurrentTable.IsPublic == isPublic)
                                                      .FirstOrDefault();

                return (currentDoc == null);
            }
        }

    }
}
